/**********************************************
 * 
 * File Name				:	UserManagementServiceImpl.java
 * 
 * Last Changed Date		: 	$Date: 2009-08-06 14:51:00 +0800 (星期四, 06 八月 2009) $
 * 
 * Last Changed Revision	:	$Revision: 92 $
 * 
 * Last Changed Author		:	$Author: cfanboy $
 * 
 * File Header URL			:	$HeadURL: svn://www.svnhost.cn/Busi-Building/code/branch/src/com/busibuilding/administrate/security/services/UserManagementServiceImpl.java $
 * 
 * ********************************************
 */

package com.busibuilding.administrate.security.services;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.transaction.annotation.Transactional;

import com.busibuilding.administrate.security.dao.UserManagementDAO;
import com.busibuilding.administrate.security.vo.UserCVO;
import com.busibuilding.common.config.sqlmap.entities.MenuVO;
import com.busibuilding.common.exceptions.DAOException;
import com.busibuilding.common.exceptions.ServiceException;
import com.busibuilding.common.services.CommonServicesImpl;
import com.busibuilding.common.vo.ICommonVO;

/**
 * Class Name: UserManagementServiceImpl.java
 * Description: 
 * @author YU Zhipeng
 * @version 1.0
 * @since 1.0
 * Date: 2009-7-20
 */
@Transactional(rollbackForClassName={"Exception"})
public class UserManagementServiceImpl extends CommonServicesImpl implements
		UserManagementService {
	private static Logger logger = LogManager.getLogger(UserManagementServiceImpl.class);
	
	private UserManagementDAO dao;
	
	public void setDao(UserManagementDAO dao) {
		this.dao = dao;
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 8855601947976181855L;

	@Override
	public int createUser(ICommonVO vo) throws ServiceException {
		int id = SQLMAP_COMMON_RESULT_ERROR;
		try {
			id = dao.insert(vo);
		} catch (DAOException e) {
			logger.fatal(e.getErrorCode() + ":" + e.getErrorMessage());
			throw new ServiceException(SERVICE_ERROR_CODE_SECURITY_CREATE_USER, e);
		} catch (Exception e) {
			logger.fatal(e);
			throw new ServiceException(SERVICE_ERROR_CODE_SECURITY_CREATE_USER, e);
		}
		return id;
	}

	@Override
	public int deleteUser(ICommonVO vo) throws ServiceException {
		int id = SQLMAP_COMMON_RESULT_ERROR;
		try {
			id = dao.delete(vo);
		} catch (DAOException e) {
			logger.fatal(e.getErrorCode() + ":" + e.getErrorMessage());
			throw new ServiceException(SERVICE_ERROR_CODE_SECURITY_DELETE_USER, e);
		} catch (Exception e) {
			logger.fatal(e);
			throw new ServiceException(SERVICE_ERROR_CODE_SECURITY_DELETE_USER, e);
		}
		return id;
	}

	@Override
	public List<UserCVO> searchUserByCondition(ICommonVO vo)
			throws ServiceException {
		List<UserCVO> resultList = new ArrayList<UserCVO>();
		try {
			resultList = dao.searchUserByCondition(vo);
		} catch (DAOException e) {
			logger.fatal(e.getErrorCode() + ":" + e.getErrorMessage());
			throw new ServiceException(SERVICE_ERROR_CODE_SECURITY_SEARCH_USER, e);
		} catch (Exception e) {
			logger.fatal(e);
			throw new ServiceException(SERVICE_ERROR_CODE_SECURITY_SEARCH_USER, e);
		}
		return resultList;
	}

	@Override
	public int updateUser(ICommonVO vo) throws ServiceException {
		int id = SQLMAP_COMMON_RESULT_ERROR;
		try {
			id = dao.update(vo);
		} catch (DAOException e) {
			logger.fatal(e.getErrorCode() + ":" + e.getErrorMessage());
			throw new ServiceException(SERVICE_ERROR_CODE_SECURITY_UPDATE_USER, e);
		} catch (Exception e) {
			logger.fatal(e);
			throw new ServiceException(SERVICE_ERROR_CODE_SECURITY_UPDATE_USER, e);
		}
		return id;
	}

	@Override
	public List<UserCVO> searchUserByCondition(ICommonVO vo, int skipResults,
			int maxResults) throws ServiceException {
		List<UserCVO> resultList = new ArrayList<UserCVO>();
		try {
			resultList = dao.searchUserByCondition(vo, skipResults, maxResults);
		} catch (DAOException e) {
			logger.fatal(e.getErrorCode() + ":" + e.getErrorMessage());
			throw new ServiceException(SERVICE_ERROR_CODE_SECURITY_SEARCH_USER, e);
		} catch (Exception e) {
			logger.fatal(e);
			throw new ServiceException(SERVICE_ERROR_CODE_SECURITY_SEARCH_USER, e);
		}
		return resultList;
	}

	@Override
	public int getRecordsCount(ICommonVO vo) throws ServiceException {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<MenuVO> getBelongToUserMenus(ICommonVO vo)
			throws ServiceException {
		List<MenuVO> resultList = new ArrayList<MenuVO>();
		try {
			resultList = dao.getBelongToUserMenus(vo);
		} catch (DAOException e) {
			logger.fatal(e.getErrorCode() + ":" + e.getErrorMessage());
			throw new ServiceException(SERVICE_ERROR_CODE_SECURITY_GET_USER_MENUS, e);
		} catch (Exception e) {
			logger.fatal(e);
			throw new ServiceException(SERVICE_ERROR_CODE_SECURITY_GET_USER_MENUS, e);
		}
		return resultList;
	}

	
}
